<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class diary extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	public function __construct() {
		parent::__construct();
		ini_set('session.cookie_httponly',1); 
		ini_set('session.use_only_cookies',1);	
		$this->load->model('diary_class');
		$this->load->model('user_class');
	}	 
	public function save_diary(){
		$post = $this->input->post();
		$a = @split('%3D', @base64_decode($post['intUserID']));
		if($a[0]=='user_id'){
			$post['intUserID'] = $a['1'];
		}
		$message = '';
		$diary = $this->diary_class->get_diary($post);
		if($diary!=0){
			$message = $this->diary_class->update($post);
		} else {
			$message = $this->diary_class->insert($post);
		}
		echo json_encode($message);		
	}
	public function list_diary(){
// 		$post = $this->input->post();
// 		$data=array();
// 		$a = @split('%3D', @base64_decode($post['intUserID']));
// 		if($a[0]=='user_id'){
// 			$post['intUserID'] = $a['1'];
// 		}
// 		$diary_list = $this->diary_class->list_diary($post);
// 		//$data['qry']=$this->db->last_query();
// 		if($diary_list!=0){
// 			foreach($diary_list as $key=>$value){
// 				$data['intID'][$key]=$value['intID'];
// 				$data['strTitle'][$key]=$value['strTitle'];
// 				$data['strNotes'][$key]=$value['strNotes'];
// 				$data['strImgLink'][$key]=$value['strImgLink'];
// 			}
// 		}
// 		echo json_encode($data);	
		$post = $this->input->post();
		$data=array();
		$a = @split('%3D', @base64_decode($post['intUserID']));
		if($a[0]=='user_id'){
			$post['intUserID'] = $a['1'];
		}
		$diary_list = $this->diary_class->list_diary($post);
		if($diary_list!=0){
			foreach($diary_list as $key=>$value){
				$data['strUserName'][$key]='';
				$data['intID'][$key]=$value['intID'];
				$data['intUserID'][$key]=$value['intUserID'];
				$post['intUserID']=$value['intUserID'];
				$user = $this->user_class->get_user_data($post);
				$data['test']=$user;
				if($user!=0){
					$data['strUserName'][$key]=$user['strName'];
				}
				$data['strTitle'][$key]=$value['strTitle'];
				$data['strNotes'][$key]=$value['strNotes'];
				$data['strImgLink'][$key]=$value['strImgLink'];
				$data['intDisabled'][$key]=$value['intDisabled'];
				$data['dateCreatedDate'][$key]=$value['dateCreatedDate'];
			}
		}
		echo json_encode($data);			
	}
	public function get_diary(){
		$post = $this->input->post();
		$diary = $this->diary_class->get_diary($post);
		echo json_encode($diary);
	}
	public function delete_diary(){
		$post = $this->input->post();
		$message = $this->diary_class->delete($post);
		echo json_encode($message);
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */